<?php


/**
 * @author Admin
 *
 *
 */
class GalleryDao extends ContentDao {
	const POPUPWidth=650;
	
	const POPUPHeight=570;
	//TODO - Insert your code here
	/***
	 * The root controller
	 */
	protected $controller;
	/***
	 * The model for loading image data
	 */
	protected $model;
	/**
	 * 
	 * Constructor
	 * @param unknown_type $action
	 */
	function __construct($action = FALSE) {
		$this->property_types = array_merge_recursive(
		$this->property_types,
		array(
                       'catId' => array(
                                                 'type' => 'INT',
                                                 'constraint' => 5,
												 'default' => 0
                                          ),
						'metadata' => array(
                                                 'type' => 'TEXT',
                                          		 'null' => TRUE
                                          ),
                       'selectedIds' => array(
                                                 'type' => 'TEXT',
                                                 'null' => TRUE,
                                          		 'constraint' => 1000,
                                          		 'customType' => 'list'
                                          ),
                        'title' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => 255,
	                     						 'null' => TRUE
                                          )                  
                                          ,
                       'defaultThumbsize' => array(
                                                 'type' => 'INT',
                                                 'constraint' => 5,
												 'default' => 0
                                          )
                )
		); 
		parent::__construct( $action );
		$this->controller = CI_Controller::get_instance();
		$this->model = $this->controller->load->model( 'GalleryModel','gallerymodel' );
		//TODO - Insert your code here
	}

	/**
	 *
	 * @see ContentDao::GetEditorDisplay()
	 */
	public  function GetEditorDisplay() {
		$categories = $this->controller->gallerymodel->getCategories();
		foreach ( $categories as $category ){
			$imgNum = $this->controller->gallerymodel->countImagesInCategory( $category->id );
			$category->imgs = $this->controller->gallerymodel->getImages( $category->id,$imgNum,0 );
		}
		$display = new Display();
		$display->headerTxt = "<h5 class='aligncenter'>Please choose a category and select the images you want in your gallery</h5>";
		$display->scripts = '';
		$category_id = Request::prop( 'catId' );
		if(!Isset($category_id)){
			$category_id=$this->catId;
		}
		$display->catId = $category_id;
		$display->content_id = $this->content_id;
		$display->base = "ContentDaoTemplates/gallery";
		$display->section_type = ContentLineManager::GALLERY;
		if( $this->selectedIds instanceof StringCollection && $this->selectedIds->size() > 0 ){
			$selected = $this->selectedIds->toString();
		}
		else {
			$selected = "";
		}
		$defaultThumbsize=(!empty($this->defaultThumbsize))?90:$this->defaultThumbsize;
		$display->defaultThumbsize = $defaultThumbsize;
		$gallery = new Gallery(Gallery::CONFIGURE,Gallery::MULTI,$defaultThumbsize,$this->catId,ContentLineManager::GALLERY,$config = array(
				"selectedIds" => $selected,
				"img_per_page" => 20,
				"extraNavContent" => "contentId/$this->content_id/daoId/$this->oid",
				"thumb" => true
			) );
		$display->imgs = $gallery->display->content;
		$display->galleryTitle = $this->title;
		return $display;
		//TODO - Insert your code here
	}
	
	/**
	 * Gets a display for showing view with edit controls
	 * (non-PHPdoc)
	 * @see ContentDao::GetEditableDisplay()
	 */
	public function GetEditableDisplay( $section_type="" ){
		$display = parent::GetEditableDisplay();
		if( $this->selectedIds instanceof StringCollection && $this->selectedIds->size() > 0 ){
			$selected = $this->selectedIds->toString();
		}
		else {
			$selected = "";
		}
		$gallery = new ViewGallery(Gallery::VIEW,Gallery::MULTI,$this->defaultThumbsize,$this->catId,ContentLineManager::GALLERY,$config = array(
				"selectedIds" => $selected,
				"img_per_page" => 20
			) );
		$galleryWrapper = new Display();
		$galleryWrapper->base = "ContentDaoTemplates/galleryWrapper";
		$galleryWrapper->gallery = $gallery->display->content;
		$galleryWrapper->galleryTitle = $this->title;
		$galleryWrapper->defaultThumbsize = $this->defaultThumbsize;
		$galleryWrapper->finalize();
		$display = parent::GetEditableDisplay("",$galleryWrapper->content);
		$display->section_type = ContentLineManager::GALLERY;
		return $display->output->content;
	}
	
	/**
	 * 
	 * get the content for the section to view on the site
	 */
	public function GetDisplay(){
			if( (bool)$this->publishable ){
				if( $this->selectedIds instanceof StringCollection && $this->selectedIds->size() > 0 ){
				$selected = $this->selectedIds->toString();
			}
			else {
				$selected = "";
			}
			$sentThumbSize = Request::prop('defaultThumbsize');
			if(!empty($sentThumbSize)){
				$_SESSION['thumbnail_sizes']=$sentThumbSize;
			}
			if( empty($_SESSION['thumbnail_sizes'])){
				$_SESSION['thumbnail_sizes']=$this->defaultThumbsize;
			}
			
			$gallery = new ViewGallery(Gallery::VIEW,Gallery::MULTI,$_SESSION['thumbnail_sizes'],$this->catId,ContentLineManager::GALLERY,$config = array(
				"selectedIds" => $selected,
				"img_per_page" => 20
			) );
			$galleryWrapper = new Display();
			$galleryWrapper->base = "ContentDaoTemplates/galleryWrapper";
			$galleryWrapper->gallery = $gallery->display->content;
			$galleryWrapper->galleryTitle = $this->title;
			$galleryWrapper->defaultThumbsize = $_SESSION['thumbnail_sizes'];
			$galleryWrapper->addJSInit( " $(document).ready( function() {
			 $('#defaultThumbsize').autoSubmit();console.log( $('#defaultThumbsize') );
			 });
			 " );
			$galleryWrapper->finalize();
			return $galleryWrapper->content;
		}
		return "";
	}

}


?>